package com.ruoyi.web.controller.edh;

import com.ruoyi.common.annotation.DataScope;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.edh.domain.EdhGoods;
import com.ruoyi.edh.domain.dto.EdhAddGoodsDto;
import com.ruoyi.edh.domain.dto.EdhSummaryGoodsDto;
import com.ruoyi.edh.domain.vo.EdhDetailsGoodsVo;
import com.ruoyi.edh.domain.vo.EdhSummaryGoodsListVo;
import com.ruoyi.edh.domain.vo.EdhSummaryGoodsVo;
import com.ruoyi.edh.service.IEdhGoodsService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 易订货-商品Controller
 *
 * @author xxx
 * @date 2020-09-02
 */
@RestController
@RequestMapping("/edh/goods")
@Api(description = "易订货-商品")
public class EdhGoodsController extends BaseController {
    @Autowired
    private IEdhGoodsService edhGoodsService;

    /**
     * 查询易订货-商品列表
     */
    @GetMapping("/list")
    @ApiOperation("易订货-商品列表")
    @DataScope(deptAlias = "d", userAlias = "u")
    public TableDataInfo list(EdhGoods edhGoods) {
        startPage();
        List<EdhSummaryGoodsListVo> list = edhGoodsService.selectEdhGoodsList(edhGoods);
        return getDataTable(list);
    }

    /**
     * 查询易订货-商品列表
     */
    @GetMapping("/summarygoodslist")
    @ApiOperation("易订货-商品列表")
    public TableDataInfo selectSummaryGoodsList(EdhSummaryGoodsDto edhSummaryGoodsDto) {
        startPage();
        List<EdhSummaryGoodsVo> list = edhGoodsService.selectSummaryGoodsList(edhSummaryGoodsDto);
        return getDataTable(list);
    }

    @GetMapping("/selectDetailsGoodsByid")
    @ApiOperation("商品根据id查询详情接口")
    @ApiParam(name = "id", value = "商品id")
    public AjaxResult selectDetailsGoodsByid(String id) {
        EdhDetailsGoodsVo edhDetailsGoodsVo = edhGoodsService.selectDetailsGoodsByid(id);
        if (null == edhDetailsGoodsVo) {
            return AjaxResult.error("没有当前商品的详细信息");
        }
        return AjaxResult.success("查询成功", edhDetailsGoodsVo);
    }

    /**
     * 导出易订货-商品列表
     */
    @PreAuthorize("@ss.hasPermi('system:goods:export')")
    @Log(title = "易订货-商品", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    @ApiOperation("导出易订货-商品列表")
    public AjaxResult export(EdhGoods edhGoods) {
        List<EdhSummaryGoodsListVo> list = edhGoodsService.selectEdhGoodsList(edhGoods);
        ExcelUtil<EdhSummaryGoodsListVo> util = new ExcelUtil<EdhSummaryGoodsListVo>(EdhSummaryGoodsListVo.class);
        return util.exportExcel(list, "goods");
    }

    /**
     * 获取易订货-商品详细信息
     */
    @PreAuthorize("@ss.hasPermi('system:goods:query')")
    @GetMapping
    @ApiOperation("获取易订货-商品详细信息")
    public AjaxResult getInfo(Long id) {
        return AjaxResult.success(edhGoodsService.selectEdhGoodsById(id));
    }

    /**
     * 新增易订货-商品
     */
    @PreAuthorize("@ss.hasPermi('system:goods:add')")
    @Log(title = "易订货-商品", businessType = BusinessType.INSERT)
    @PostMapping
    @ApiOperation("新增易订货-商品")
    public AjaxResult add(@RequestBody EdhAddGoodsDto edhGoods) {
        if (StringUtils.isNull(edhGoods.getImage())) {
            return AjaxResult.error("图片至少上传一张");
        }
        Boolean aBoolean = edhGoodsService.insertEdhGoods(edhGoods);
        if (aBoolean) {
            return AjaxResult.success("添加成功");
        }
        return AjaxResult.error("添加失败");
    }

    /**
     * 修改易订货-商品
     */
    @PreAuthorize("@ss.hasPermi('system:goods:edit')")
    @Log(title = "易订货-商品", businessType = BusinessType.UPDATE)
    @PutMapping
    @ApiOperation("修改易订货-商品")
    public AjaxResult edit(@RequestBody EdhAddGoodsDto edhGoods) {
        if (StringUtils.isNull(edhGoods.getImage())) {
            return AjaxResult.error("图片至少上传一张");
        }
        Boolean aBoolean = edhGoodsService.updateEdhGoods(edhGoods);
        if (aBoolean) {
            return AjaxResult.success("修改成功");
        }
        return AjaxResult.error("修改失败");
    }

    /**
     * 修改易订货-商品
     */
    @PreAuthorize("@ss.hasPermi('system:goods:edit')")
    @Log(title = "易订货-商品", businessType = BusinessType.UPDATE)
    @PutMapping("/editOnsale")
    @ApiOperation("修改易订货-商品")
    public AjaxResult editOnsale(@RequestParam("ids") Long[] ids, @RequestParam("onSale") String onSale) {
        return toAjax(edhGoodsService.editOnsale(ids, onSale));
    }

    /**
     * 删除易订货-商品
     */
    @PreAuthorize("@ss.hasPermi('system:goods:remove')")
    @Log(title = "易订货-商品", businessType = BusinessType.DELETE)
    @DeleteMapping("/remove")
    @ApiOperation("删除易订货-商品")
    public AjaxResult remove(@RequestParam("ids") Long[] ids) {
        return toAjax(edhGoodsService.deleteEdhGoodsByIds(ids));
    }

    /**
     * 获取首页商品总览数据
     */
    @GetMapping("/getProductOverview")
    @ApiOperation("获取首页商品总览数据")
    public AjaxResult getProductOverview() {
        return AjaxResult.success(edhGoodsService.getProductOverview());
    }

    /**
     * 获取根据分类的商品tree
     */
    @GetMapping("/getGoodsTree")
    @ApiOperation("获取根据分类的商品tree")
    public AjaxResult getGoodsTree(Long scheduledId) {
        return AjaxResult.success(edhGoodsService.getGoodsTree(scheduledId));
    }

    /**
     * @param edhGoods
     * @description: 修改商品
     * @return: AjaxResult
     * @author: 梁东宸
     * @time: 2020/9/24 2:17 下午
     */
    @PutMapping("/editGoods")
    @ApiOperation("修改商品")
    public AjaxResult updateEdhGoodsState(@RequestBody EdhGoods edhGoods) {
        return toAjax(edhGoodsService.updateEdhGoodsState(edhGoods));
    }

}
